package com.xingdata.jjxc.m.avt;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;

import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.adapter.CarInfomationAdp;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.enty.CarhudEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.m.interf.CarInfoItemClick;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.ExitApplication;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SP;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.views.Widget;

public class CarInfomatavt extends BaseActivity implements CarInfoItemClick {

	private GridView car_infomation_gv;

	private CarInfomationAdp carAdapter;

	private CarhudEntity carInfoEn;
	private List<CarhudEntity> carhudList = new ArrayList<CarhudEntity>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		ExitApplication.getInstance().addActivity(this);
		carhudList = SP.getHudInfo(this);
		doPost_carList();

	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_car_infomatavt;
	}

	@Override
	protected String getTitleText() {
		return JUtils.TITILE_CARINFOMATION;
	}

	@Override
	protected void initViews() {
		car_infomation_gv = (GridView) findViewById(R.id.car_infomation_gv);
	}

	@Override
	public void onResume() {

		carhudList = SP.getHudInfo(this);
		doPost_carList();
		super.onResume();
	}

	private void doPost_carList() {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		resp = null;
		params = new AbRequestParams();
		params.put("mobile", SystemInfo.getUserInfo().getMobile());
		params.put("password", SystemInfo.getUserInfo().getPw());

		mAbHttpUtil.post(App.ZZD_REQUEST_LOGIN, params,
				new AbStringHttpResponseListener() {

					@Override
					public void onSuccess(int statusCode, String content) {
						resp = JSON.parseObject(content, RespEntity.class);
						Log.i("LOGIN", content);
					}

					// 开始执行前
					@Override
					public void onStart() {

						showProgressDialog("加载中，请稍等");
					}

					// 失败，调用
					@Override
					public void onFailure(int statusCode, String content,
							Throwable error) {
						Log.i("LOG", error.getMessage());
					}

					// 完成后调用，失败，成功
					@Override
					public void onFinish() {
						if (resp != null) {
							if (resp.getState() == 0) {

								carhudList = JUtils.getcarHudList(resp
										.getHudlist());
								carAdapter = new CarInfomationAdp(
										CarInfomatavt.this, carhudList,
										CarInfomatavt.this,car_infomation_gv);
								car_infomation_gv.setAdapter(carAdapter);

								SP.saveHudInfo(CarInfomatavt.this,
										resp.getHudlist());
							} else if (resp.getState() == 1) {
								showToast(resp.getMsg());
							} else {
								showToast(resp.getMsg());
							}
							removeProgressDialog();
						} else {
							showToast("网络超时，请重试");
							removeProgressDialog();
							return;
						}

					};

				});
	}

	private void doPost_deleteLoveBus(String car_id, String hudsn,
			String car_number, String enqine_number, String underpan_number) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		resp = null;
		params = new AbRequestParams();

		params.put("access_token", Code.accessToken());
		params.put("userid", SystemInfo.getUserInfo().getUserid());
		params.put("car_id", car_id);
		params.put("hudsn", hudsn);
		params.put("car_number", car_number);
		params.put("engine_number", enqine_number);
		params.put("underpan_number", underpan_number);
		params.put("flag", "9");

		mAbHttpUtil.post(App.ZZD_REQUEST_CARHUD, params,
				new AbStringHttpResponseListener() {
					@Override
					public void onSuccess(int statusCode, String content) {
						resp = JSON.parseObject(content, RespEntity.class);
					}

					@Override
					public void onFailure(int statusCode, String content,
							Throwable error) {
					}

					@Override
					public void onStart() {
						// 显示进度框
						showProgressDialog();
					}

					@Override
					public void onFinish() {
						if (resp != null) {
							if (resp.getState() == 0) {
								showToast("删除车辆成功");
								doPost_carList();
							} else if (resp.getState() == 1) {
								showToast(resp.getMsg());
							} else {
								showToast(resp.getMsg());
							}
							removeProgressDialog();
						} else {
							showToast("网络超时，请重试");
							removeProgressDialog();
							return;
						}
					}
				});
	}

	@Override
	public void deleteClick(final CarhudEntity carEn, ImageView delete_iv,
			final int position) {
		delete_iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				doPost_deleteLoveBus(carEn.getCar_id(), carEn.getHudsn(),
						carEn.getCar_number(), carEn.getEngine_number(),
						carEn.getUnderpan_number());
			}
		});
	}
	@Override
	public void onIntentClick(CarhudEntity carInfoEn, int position) {
		Intent intent = new Intent(CarInfomatavt.this, EditLovecar.class);
		Bundle bundle = new Bundle();

		bundle.putSerializable("car_id", carInfoEn.getCar_id());
		bundle.putSerializable("abbreviate", "简称未知");
		bundle.putSerializable("hud_num", carInfoEn.getHudsn());
		bundle.putSerializable("natholith", carInfoEn.getUnderpan_number());
		bundle.putSerializable("car_num", carInfoEn.getCar_number());
		bundle.putSerializable("engine_number", carInfoEn.getEngine_number());
		bundle.putSerializable("frame_number", "车架号未知");
		bundle.putSerializable("car_name", "车名未知");
		bundle.putSerializable("car_state", "车厢样式未知");
		intent.putExtras(bundle);
		startActivity(intent);
		Widget.startActivityAnim(CarInfomatavt.this,
				R.anim.avty_slide_in_from_right,
				R.anim.avty_slide_out_from_left);

	}

}
